package Bean;
import java.io.*;

public class UserBean {
    private String username;
    private String password1;
    private String password2;
    private String email;
    private String name;
    private String group;
    private String head;

    public UserBean() {
        username=null;
        password1=null;
        password2=null;
        email=null;
        name=null;
        group=null;
        head="unset.jpg";
    }

    public String getusername() {
        return username;
    }
    public void setusername(String username) {
        this.username= username;
    }

    public String getpassword1() {
        return password1;
    }
    public void setpassword1(String password1) {
        this.password1= password1;
    }

    public String getpassword2() {
        return password2;
    }
    public void setpassword2(String password2) {
        this.password2= password2;
    }

    public String getemail() {
        return email;
    }
    public void setemail(String email) {
        this.email= email;
    }

    public String getname() {
        return name;
    }
    public void setname(String name) {
        this.name= name;
    }

    public String getgroup() { return group; }
    public void setgroup(String group) { this.group= group; }

    public String gethead() { return head; }
    public void sethead(String head) { this.head= head; }

    //登录验证
    public boolean check_login(){
        boolean isValid = false;
        try{
        //读取文件(字符流)
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("user.txt")));
            try {
                String str;
                String[] StrArray;
                String temp_username;
                String temp_password;
                while ((str = in.readLine()) !=null) {
                    StrArray = str.split("---");
                    temp_username=StrArray[0];
                    if(temp_username.equals(username)) {
                        temp_password = StrArray[1];
                        if (temp_password.equals(password1)) {
                            isValid = true;
                            name=StrArray[2];
                            group=StrArray[3];
                            email=StrArray[4];
                            head=StrArray[5];
                        }
                    }
                }
                //关闭流
                in.close();
            } catch (IOException e){ }
        } catch (FileNotFoundException e) { }
        return isValid;
    }

    //登陆输入空验证
    public boolean isNull_log(){
        boolean isValid = false;
        if(username==null||"".equals(username)||password1==null||"".equals(password1)){
            isValid=true;
        }
        return isValid;
    }

    //注册验证
    public boolean check_register(){
        boolean isValid = false;
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("user.txt")));
            try {
                String str;
                String[] StrArray;
                String temp_username;
                while ((str = in.readLine()) !=null) {
                    StrArray = str.split("---");
                    temp_username=StrArray[0];
                    if(temp_username.equals(username))
                            isValid=true;
                }
                in.close();
            } catch (IOException e){ }
        } catch (FileNotFoundException e) { }
        return isValid;
    }

    //添加注册用户
    public boolean add(){
        boolean isValid = false;
        try{
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("user.txt",true)));
            try {
                String user_info=username+"---"+password1+"---"+name+"---"+group+"---"+email+"---"+head+"\n";
                out.write(user_info);
                isValid=true;
                out.close();
            } catch (IOException e){ }
        } catch (FileNotFoundException e) { }
        return isValid;
    }
    //注册输入空验证
    public boolean isNull_reg(){
        boolean isValid = false;
        if(username==null||"".equals(username)||password1==null||"".equals(password1)|| password2==null ||"".equals(password2)||email==null|| "".equals(email)|| name==null||"".equals(name)||email==group|| "".equals(group)){
            isValid=true;
        }
        return isValid;
    }
    //两次输入密码验证
    public boolean isEqual(){
        boolean isValid = false;
        if(password1.equals(password2)){
            isValid=true;
        }
        return isValid;
    }

}
